package design.pattern.Lu02FactoryPattern;

/**
 * @program JavaBase
 * @description: 工厂模式
 * @author: zhanglu
 * @create: 2020-03-27 12:44:00
 */
public class Lu02FactoryPattern2 {

    public static void main(String[] args) {
        ConcreteCreator concreteCreator = new Creator();

        Product01 product = concreteCreator.createProduct(Product01.class);

        product.todo();
    }

}

abstract class Product{

    String name;

    abstract void todo();

}

class Product01 extends Product{

    @Override
    void todo() {
        System.out.println("我是" + name + ",我可以做很多事情");
    }

}

class Product02 extends Product{

    @Override
    void todo() {
        System.out.println("我是" + name + ",我只能用一次");
    }

}

abstract class ConcreteCreator{

    public abstract <T extends Product> T createProduct(Class<T> tClass);

}

class Creator extends ConcreteCreator{

    @Override
    public <T extends Product> T createProduct(Class<T> tClass) {

        T t = null;
        try {
            t = tClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return t;
    }

}



